Objectives

  • Introduce plot_ly() generating interactive graphs
  • Introduce shiny for interactive visualizations
  • Introduce flexdashboard for creating interactive dashboards
library(tidyverse)
library(knitr)
library(broom)
library(stringr)
library(modelr)
library(forcats)
library(haven)
library(plotly)

options(digits = 3)
set.seed(1234)
theme_set(theme_minimal())

Plotly

We previously discussed using plotly and the ggplotly() function to modify existing ggplot() objects. However we can also build plotly.js objects directly using the plot_ly() function. It is based on the layered grammar of graphics, but instead it directly calls components of the plotly.js JavaScript library to generate graphs. Originally it was intended to be used for plot types not supported by ggplot2 (like 3d graphs), but has been extended so that you could use it entirely instead of ggplot2.

txhousing
## # A tibble: 8,602 × 9
##       city  year month sales   volume median listings inventory  date
##      <chr> <int> <int> <dbl>    <dbl>  <dbl>    <dbl>     <dbl> <dbl>
##  1 Abilene  2000     1    72  5380000  71400      701       6.3  2000
##  2 Abilene  2000     2    98  6505000  58700      746       6.6  2000
##  3 Abilene  2000     3   130  9285000  58100      784       6.8  2000
##  4 Abilene  2000     4    98  9730000  68600      785       6.9  2000
##  5 Abilene  2000     5   141 10590000  67300      794       6.8  2000
##  6 Abilene  2000     6   156 13910000  66900      780       6.6  2000
##  7 Abilene  2000     7   152 12635000  73500      742       6.2  2000
##  8 Abilene  2000     8   131 10710000  75000      765       6.4  2001
##  9 Abilene  2000     9   104  7615000  64500      771       6.5  2001
## 10 Abilene  2000    10   101  7040000  59300      764       6.6  2001
## # ... with 8,592 more rows
# ggplot
p <- ggplot(txhousing, aes(date, median)) +
  geom_line(aes(group = city), alpha = 0.2)
ggplotly(p)
# initialize plotly object
p <- txhousing %>%
  group_by(city) %>%
  plot_ly(x = ~date, y = ~median)
p
# specify geometric relation (i.e. what kind of geom to draw?)
# add a line highlighting houston
add_lines(
  # plots one line per city since p knows city is a grouping variable
  add_lines(p, alpha = 0.2, name = "Texan Cities", hoverinfo = "none"),
  name = "Houston", data = filter(txhousing, city == "Houston")
)
# redone completely with piping
allCities <- txhousing %>%
  group_by(city) %>%
  plot_ly(x = ~date, y = ~median) %>%
  add_lines(alpha = 0.2, name = "Texan Cities", hoverinfo = "none")

allCities %>%
  filter(city == "Houston") %>%
  add_lines(name = "Houston")

We can add “layers” by using the piped notation used by dplyr and other tidyverse packages (not + in ggplot2).

More support for plotly

Shiny

See here.

flexdashboard

flexdashboard is a package for R that allows you to create visual dashboards. You can incorporate tables, graphs (both static and dynamic), text, and all sorts of components to provide insight and analysis. Layouts are customizable by dividing the page into columns and rows.

The entire dashboard is basically one large R Markdown document, so you can use the same syntax and code/output/analysis format that you’ve used before (though generally hiding the code with echo = FALSE).

Review layout and components

Examples of flexdashboards

Shiny with flexdashboard

One concern with a traditional dashboard is that while it may be “interactive”, it is not dynamic - that is, you cannot update the dashboard by changing parameters or underlying data values. Shiny applications allow you to do so, but they are very clunky to write due to the deeply nested structure of the code. Instead, we can combine the two approaches by generating a Shiny dashboard. In our header, we simply add runtime: shiny to convert the standard dashboard to a Shiny dashboard.

Examples of Shiny flexdashboards

Session Info

devtools::session_info()
##  setting  value                       
##  version  R version 3.3.3 (2017-03-06)
##  system   x86_64, darwin13.4.0        
##  ui       X11                         
##  language (EN)                        
##  collate  en_US.UTF-8                 
##  tz       America/Chicago             
##  date     2017-05-03                  
## 
##  package     * version    date       source                           
##  assertthat    0.2.0      2017-04-11 cran (@0.2.0)                    
##  backports     1.0.5      2017-01-18 CRAN (R 3.3.2)                   
##  broom       * 0.4.2      2017-02-13 CRAN (R 3.3.2)                   
##  colorspace    1.3-2      2016-12-14 CRAN (R 3.3.2)                   
##  DBI           0.6        2017-03-09 CRAN (R 3.3.3)                   
##  devtools      1.12.0     2016-06-24 CRAN (R 3.3.0)                   
##  digest        0.6.12     2017-01-27 CRAN (R 3.3.2)                   
##  dplyr       * 0.5.0      2016-06-24 CRAN (R 3.3.0)                   
##  evaluate      0.10       2016-10-11 CRAN (R 3.3.0)                   
##  forcats     * 0.2.0      2017-01-23 CRAN (R 3.3.2)                   
##  foreign       0.8-67     2016-09-13 CRAN (R 3.3.3)                   
##  ggplot2     * 2.2.1.9000 2017-05-01 Github (hadley/ggplot2@f4398b6)  
##  gtable        0.2.0      2016-02-26 CRAN (R 3.3.0)                   
##  haven       * 1.0.0      2016-09-23 cran (@1.0.0)                    
##  hms           0.3        2016-11-22 CRAN (R 3.3.2)                   
##  htmltools     0.3.6      2017-04-28 cran (@0.3.6)                    
##  htmlwidgets   0.8        2016-11-09 CRAN (R 3.3.1)                   
##  httr          1.2.1      2016-07-03 CRAN (R 3.3.0)                   
##  jsonlite      1.4        2017-04-08 cran (@1.4)                      
##  knitr       * 1.15.1     2016-11-22 cran (@1.15.1)                   
##  lattice       0.20-34    2016-09-06 CRAN (R 3.3.3)                   
##  lazyeval      0.2.0      2016-06-12 CRAN (R 3.3.0)                   
##  lubridate     1.6.0      2016-09-13 CRAN (R 3.3.0)                   
##  magrittr      1.5        2014-11-22 CRAN (R 3.3.0)                   
##  memoise       1.0.0      2016-01-29 CRAN (R 3.3.0)                   
##  mnormt        1.5-5      2016-10-15 CRAN (R 3.3.0)                   
##  modelr      * 0.1.0      2016-08-31 CRAN (R 3.3.0)                   
##  munsell       0.4.3      2016-02-13 CRAN (R 3.3.0)                   
##  nlme          3.1-131    2017-02-06 CRAN (R 3.3.3)                   
##  plotly      * 4.6.0      2017-04-25 CRAN (R 3.3.3)                   
##  plyr          1.8.4      2016-06-08 CRAN (R 3.3.0)                   
##  psych         1.7.3.21   2017-03-22 CRAN (R 3.3.2)                   
##  purrr       * 0.2.2      2016-06-18 CRAN (R 3.3.0)                   
##  R6            2.2.0      2016-10-05 CRAN (R 3.3.0)                   
##  Rcpp          0.12.10    2017-03-19 cran (@0.12.10)                  
##  readr       * 1.1.0      2017-03-22 cran (@1.1.0)                    
##  readxl        0.1.1      2016-03-28 CRAN (R 3.3.0)                   
##  reshape2      1.4.2      2016-10-22 CRAN (R 3.3.0)                   
##  rlang         0.0.0.9018 2017-05-01 Github (hadley/rlang@460323e)    
##  rmarkdown     1.3        2016-12-21 CRAN (R 3.3.2)                   
##  rprojroot     1.2        2017-01-16 CRAN (R 3.3.2)                   
##  rvest         0.3.2      2016-06-17 CRAN (R 3.3.0)                   
##  scales        0.4.1      2016-11-09 CRAN (R 3.3.1)                   
##  stringi       1.1.2      2016-10-01 CRAN (R 3.3.0)                   
##  stringr     * 1.2.0      2017-02-18 CRAN (R 3.3.2)                   
##  tibble      * 1.3.0.9001 2017-05-01 Github (tidyverse/tibble@08af6b0)
##  tidyr       * 0.6.1      2017-01-10 CRAN (R 3.3.2)                   
##  tidyverse   * 1.1.1      2017-01-27 CRAN (R 3.3.2)                   
##  viridisLite   0.2.0      2017-03-24 cran (@0.2.0)                    
##  withr         1.0.2      2016-06-20 CRAN (R 3.3.0)                   
##  xml2          1.1.1      2017-01-24 CRAN (R 3.3.2)                   
##  yaml          2.1.14     2016-11-12 cran (@2.1.14)